/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapterBindingElement.cs
/// Description :  Provides a base class for the configuration elements.
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Channels;
using System.Configuration;
using System.Globalization;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    public class SalesForceWCFAdapterBindingElement : StandardBindingElement
    {
        private ConfigurationPropertyCollection properties;

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapterBindingElement class
        /// </summary>
        public SalesForceWCFAdapterBindingElement()
            : base(null)
        {
        }


        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapterBindingElement class with a configuration name
        /// </summary>
        public SalesForceWCFAdapterBindingElement(string configurationName)
            : base(configurationName)
        {
        }

        #endregion Constructors

        #region Custom Generated Properties



        #endregion Custom Generated Properties

        #region Protected Properties

        /// <summary>
        /// Gets the type of the BindingElement
        /// </summary>
        protected override Type BindingElementType
        {
            get
            {
                return typeof(SalesForceWCFAdapterBinding);
            }
        }

        #endregion Protected Properties

        #region StandardBindingElement Members

        /// <summary>
        /// Initializes the binding with the configuration properties
        /// </summary>
        protected override void InitializeFrom(Binding binding)
        {
            base.InitializeFrom(binding);
            SalesForceWCFAdapterBinding adapterBinding = (SalesForceWCFAdapterBinding)binding;
            
            this["host"] = adapterBinding.Host;
            this["hostType"] = adapterBinding.HostType;
            this["protocol"] = adapterBinding.Protocol;
            this["securityToken"] = adapterBinding.SecurityToken;
            this["version"] = adapterBinding.Version;
            this["username"] = adapterBinding.Username;
            this["password"] = adapterBinding.Password;
        }

        /// <summary>
        /// Applies the configuration
        /// </summary>
        protected override void OnApplyConfiguration(Binding binding)
        {
            //DoMsgBox("In apply configuration.");

            if (binding == null)
            {
                //DoMsgBox("binding is null");
                throw new ArgumentNullException("binding");

            }
            SalesForceWCFAdapterBinding adapterBinding = (SalesForceWCFAdapterBinding)binding;
            adapterBinding.Host = (string)this["host"];
            adapterBinding.HostType = (HostTypeEnum)this["hostType"];
            if (this["protocol"] == null)
            {
                adapterBinding.Protocol = ProtocolTypesEnum.SOAP;
            }
            else
            {
                try
                {
                    adapterBinding.Protocol = (ProtocolTypesEnum)this["protocol"];
                }
                catch (Exception e)
                {
                    adapterBinding.Protocol = ProtocolTypesEnum.SOAP;
                }
            }
            adapterBinding.SecurityToken = (string)this["securityToken"];
            adapterBinding.Version = (string)this["version"];
            adapterBinding.Username = (string)this["username"];
            adapterBinding.Password = (string)this["password"];
        }

        /// <summary>
        /// Returns a collection of the configuration properties
        /// </summary>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
               
               ConfigurationPropertyCollection configProperties = base.Properties;

                configProperties.Add(new ConfigurationProperty("host", typeof(string), "test.salesforce.com", null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("hostType", typeof(HostTypeEnum), HostTypeEnum.u, null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("protocol", typeof(ProtocolTypesEnum), ProtocolTypesEnum.SOAP, null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("securityToken", typeof(string), "test", null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("version", typeof(string), "8.0", null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("username", typeof(string), "", null, null, ConfigurationPropertyOptions.None));
                configProperties.Add(new ConfigurationProperty("password", typeof(string), "", null, null, ConfigurationPropertyOptions.None));
                
                this.properties = configProperties;
                
                return this.properties;
            }
        }


        #endregion StandardBindingElement Members

        #region Adapter Custom Configuration

        void DoMsgBox(string s)
        {
            System.Windows.Forms.MessageBox.Show(s);
        }

        [ConfigurationProperty("host", DefaultValue = "test.salesforce.com")]
        [Category("Uri-Related")]
        public string Host
        {
            get
            {
                return (string)base["host"];
            }
            set
            {
                base["host"] = value;
            }
        }

        [ConfigurationProperty("hostType", DefaultValue = HostTypeEnum.u)]
        [Category("Uri-Related")]
        public HostTypeEnum HostType
        {
            get
            {
                return (HostTypeEnum)base["hostType"];
            }
            set
            {
                base["hostType"] = value;
            }
        }

        [ConfigurationProperty("version", DefaultValue = "8.0")]
        [Category("Uri-Related")]
        public string Version
        {
            get
            {
                return (string)base["version"];
            }
            set
            {
                base["version"] = value;
            }
        }

        [ConfigurationProperty("protocol", DefaultValue = ProtocolTypesEnum.SOAP)]
        [Category("Uri-Related")]
        public ProtocolTypesEnum Protocol
        {
            get
            {
                return (ProtocolTypesEnum)base["protocol"];
            }
            set
            {
                base["protocol"] = value;
            }
        }

        [ConfigurationProperty("securityToken", DefaultValue = "test")]
        [Category("Uri-Related")]
        public string SecurityToken
        {
            get
            {
                return (string)base["securityToken"];
            }
            set
            {
                base["securityToken"] = value;
            }
        }

        [ConfigurationProperty("username", DefaultValue="")]
        [Category("Security")]
        public string Username
        {
            get
            {
                return (string)base["username"];
            }
            set
            {
                base["username"] = value;
            }
        }


        [ConfigurationProperty("password", DefaultValue="")]
        [Category("Security")]
        public string Password
        {
            get
            {
                return (string)base["password"];
            }
            set
            {
                base["password"] = value;
            }
        }

        #endregion
    }
}
